// 短信内容
export const useSmsData = () => {
	return reactive({
		phone: '',
		loading: false,
		time: 60,
		text: '获取验证码',
		type: '', // 短信类型，1：注册,2:登录
		disabled: false,
	});
};

// 发送短信
export const sendSms = async (data) => {
	data.loading = true;
	try {
		// 这里模拟了一个延迟，用于测试发送短信功能
		await new Promise((resolve) => setTimeout(resolve, 1500));
		data.loading = false;
		data.disabled = true;
		countdownTime(data);
	} catch (error) {
		data.loading = false;
		console.error(error);
	}
};

// 倒计时
const countdownTime = (data) => {
	let timer = setInterval(() => {
		data.time--;
		data.text = `重新发送（${data.time}s）`;
		if (data.time <= 0) {
			clearInterval(timer);
			resetData(data);
		}
	}, 1000);
};

// 重置数据
const resetData = (data) => {
	data.disabled = false;
	data.text = '重新发送';
	data.time = 60;
};
